package com.dom.config;

import com.alibaba.fastjson.parser.ParserConfig;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@EnableCaching
public class RedisConf extends CachingConfigurerSupport {
//    @Autowired
//    private JedisConnectionFactory jedisConnectionFactory;
//
//    @Bean
//    @Override
//    public CacheManager cacheManager() {
//        RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager
//                .RedisCacheManagerBuilder
//                .fromConnectionFactory(jedisConnectionFactory);
//        return builder.build();
//    }

    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        //设置序列化
        FastJson2JsonRedisSerializer<Object> fastJson2JsonRedisSerializer = new FastJson2JsonRedisSerializer(Object.class);
//        ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
        ParserConfig.getGlobalInstance().addAccept("com.tamc.coin.");

        // 配置redisTemplate
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        RedisSerializer stringSerializer = new StringRedisSerializer();
        // key序列化
        redisTemplate.setKeySerializer(stringSerializer);
        // Hash key序列化
        redisTemplate.setHashKeySerializer(stringSerializer);

        // value序列化
        redisTemplate.setValueSerializer(fastJson2JsonRedisSerializer);
        // Hash value序列化
        redisTemplate.setHashValueSerializer(fastJson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }


    /**
     * 此内部类就是把yml的配置数据，进行读取，创建JedisConnectionFactory和JedisPool，以供外部类初始化缓存管理器使用
     * 不了解的同学可以去看@ConfigurationProperties和@Value的作用
     */
//    @ConfigurationProperties
//    class DataJedisProperties {
//        @Value("${spring.redis.host}")
//        private String host;
//        @Value("${spring.redis.password}")
//        private String password;
//        @Value("${spring.redis.port}")
//        private int port;
//        @Value("${spring.redis.timeout}")
//        private int timeout;
//        @Value("${spring.redis.jedis.pool.max-idle}")
//        private int maxIdle;
//        @Value("${spring.redis.jedis.pool.max-wait}")
//        private long maxWaitMillis;
//
//        @Bean
//        JedisConnectionFactory jedisConnectionFactory() {
////            lg.info("Create JedisConnectionFactory successful");
//            JedisConnectionFactory factory = new JedisConnectionFactory();
//            factory.setHostName(host);
//            factory.setPort(port);
//            factory.setTimeout(timeout);
//            factory.setPassword(password);
//            return factory;
//        }
////        @Bean
////        public JedisPool redisPoolFactory() {
//////            lg.info("JedisPool init successful，host -> [{}]；port -> [{}]", host, port);
////            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
////            jedisPoolConfig.setMaxIdle(maxIdle);
////            jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
////
////            JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
////            return jedisPool;
////        }
//    }
}
